﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace WebUI.CommonCls
{
    public class ProductImgUtils
    {
        /// <summary>
        /// 重新调整图片尺寸，产品图片默认大小为300×300（像素）。
        /// </summary>
        /// <returns></returns>
        public static void ResizeProductImg(string imgpath)
        {
            Image img = Image.FromFile(imgpath);
            Bitmap bmp = new Bitmap(300, 300);
            ResizeImage(img, bmp);
            img.Dispose();
            bmp.Save(imgpath);
            bmp.Dispose();
        }

        /// <summary>
        /// 重新调整产品封面，封面默认尺寸为150×150（像素）。
        /// </summary>
        /// <returns></returns>
        public static void ResizeProductCover(string imgpath)
        {
            Image img = Image.FromFile(imgpath);
            Bitmap bmp = new Bitmap(150, 150);
            ResizeImage(img, bmp);
            img.Dispose();
            bmp.Save(imgpath);
            bmp.Dispose();
        }

        private static void ResizeImage(Image image, Bitmap newBmp)
        {
            Graphics g = Graphics.FromImage(newBmp);
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            Rectangle srcRect = new Rectangle(0, 0, image.Width, image.Height);
            Rectangle destRect = new Rectangle(0, 0, newBmp.Width, newBmp.Height);
            g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
            g.Dispose();
        }
    }
}